package opl.utilities;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;

public class FileHelper {

	public static String ReadFileAsString(String fileName, Context context)
			throws Exception {
		AssetManager assets = context.getAssets();

		InputStream fis = assets.open(fileName);
		InputStreamReader sr = new InputStreamReader(fis);

		BufferedReader br = new BufferedReader(sr);
		StringBuffer sb = new StringBuffer();
		char[] buffer = new char[1024];
		int len1 = 0;
		while ((len1 = br.read(buffer)) != -1) {
			String buff = new String(buffer, 0, len1);
			sb.append(buff);
		}

		return new String(sb);
	}

	public static Drawable ReadFileFromResurceAsDrawable(String fileName,
			Context context) {
		try {
			Resources re = context.getResources();
			// File name hacks
			String loweredFileName = fileName.toLowerCase().replace(' ', '_');

			// re.getIdentifier("opl.ui:drawable/black_hawks",null,null)

			int resId = re.getIdentifier("opl.ui:drawable/" + loweredFileName,
					null, null);
			Drawable draws = re.getDrawable(resId);

			return draws;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
}
